<?php

namespace App\Service\ServiceTrait;

use App\Constants\BusinessCode;
use App\Exception\ApiException;
use App\Model\Guild\GuildManage;
use App\Model\GuildManage\GuildAccount;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Model;

trait GuildTrait
{

    /**
     * 确认是否有工会状态
     * @param $userId
     * @return bool
     */
    private function checkGuildStatus($userId): bool
    {
        $guildInfo = (new GuildManage)->getGuildInfoByUserId($userId);
        if (empty($guildInfo)) {
            return false;
        }

        if ($guildInfo->status == 0) {
            return false;
        }

        if ($guildInfo->guild_share_ratio > BusinessCode::GUILD_MAX_PROPORTION && $guildInfo->id != 296) {
            return false;
        }

        return true;
    }

    /**
     * 检测提现账号
     * @param $accountId
     * @param $userId
     * @return Builder|Model|object
     */
    private function checkAccount($accountId, $userId)
    {
        $info = (new GuildAccount)->where('id', $accountId)->first();
        if (empty($info)) {
            throw new ApiException('亲，请选择正确的账号');
        }

//        if ($action == 1 && $info->status != GuildAccount::STATUS_PASS){
//            throw new ApiException('您的提现账户正在审核中，审核通过后可提现～');
//        }

        if ($info->user_id != $userId) {
            throw new ApiException('无权操作该提现账号');
        }

        return $info;
    }
}